package proxy

import (
	"fmt"
	"github.com/jinzhu/gorm"
	"time"
)

//CREATE TABLE `proxy` (
//  `id` int NOT NULL,
//  `proxy_name` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `protocol` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `host` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `port` int DEFAULT NULL,
//  `user_name` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `password` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `created_at` timestamp NULL DEFAULT NULL,
//  `updated_at` timestamp NULL DEFAULT NULL,
//  `external_ip` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `country_code` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `region_code` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `city` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `country` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `user_id` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `Lat` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `Lon` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  PRIMARY KEY (`id`)
//) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

type Proxy struct {
	Id          int       `json:"id" gorm:"primary_key;column:id"`
	ProxyName   string    `json:"proxy_name" gorm:"column:proxy_name"`
	Protocol    string    `json:"protocol" gorm:"column:protocol"`
	Host        string    `json:"host" gorm:"column:host"`
	Port        int       `json:"port" gorm:"column:port"`
	UserName    string    `json:"user_name" gorm:"column:user_name"`
	Password    string    `json:"password" gorm:"column:password"`
	CreatedAt   time.Time `json:"created_at" gorm:"column:created_at"`
	UpdatedAt   time.Time `json:"updated_at" gorm:"column:updated_at"`
	ExternalIp  string    `json:"external_ip" gorm:"column:external_ip"`
	CountryCode string    `json:"country_code" gorm:"column:country_code"`
	RegionCode  string    `json:"region_code" gorm:"column:region_code"`
	City        string    `json:"city" gorm:"column:city"`
	Country     string    `json:"country" gorm:"column:country"`
	UserId      string    `json:"user_id" gorm:"column:user_id"`
	Lat         string    `json:"lat" gorm:"column:lat"`
	Lon         string    `json:"lon" gorm:"column:lon"`
	Type        string    `json:"type" gorm:"column:type"`
	Status      int       `json:"status" gorm:"column:status"`
	Timezone    string    `json:"timezone" gorm:"column:timezone"`
	Remark      string    `json:"remark" gorm:"column:remark"`
}

func (p *Proxy) TableName() string {
	return "proxy"
}

func (p *Proxy) Create(db *gorm.DB) error {
	return db.Table(p.TableName()).Create(p).Error
}
func (p *Proxy) Update(db *gorm.DB) error {
	return db.Table(p.TableName()).Where("id = ?", p.Id).Update(p).Error
}
func (p *Proxy) GetById(db *gorm.DB) error {
	return db.Table(p.TableName()).Where("id = ?", p.Id).First(p).Error
}
func (p *Proxy) GetListByUserId(db *gorm.DB, filter map[string]interface{}) ([]*Proxy, error) {
	var list []*Proxy
	obj := db.Table(p.TableName()).Where("user_id = ?", p.UserId)
	if filter != nil {
		for k, v := range filter {
			obj = obj.Where(k, v)
		}
	}
	return list, obj.Order("id asc").Find(&list).Error
}
func (p *Proxy) GetListByUserIdPaginate(db *gorm.DB, filter map[string]interface{}, page int, pageSize int) ([]*Proxy, int, error) {
	var list []*Proxy
	obj := db.Table(p.TableName()).Where("user_id = ?", p.UserId)
	if filter != nil {
		for k, v := range filter {
			fmt.Println(k, v)
			if k == "proxy_name" {
				obj = obj.Where("proxy_name like ?", "%"+v.(string)+"%")
			} else {
				obj = obj.Where(k+" = ?", v)
			}

		}
	}
	var total int
	err := obj.Count(&total).Error
	if err != nil {
		return list, total, err
	}
	err = obj.Offset((page - 1) * pageSize).Limit(pageSize).Order("id asc").Find(&list).Error
	if err != nil {
		return list, total, err
	}
	return list, total, err
}
func (p *Proxy) Delete(db *gorm.DB) error {
	return db.Table(p.TableName()).Where("id = ?", p.Id).Delete(p).Error
}
